package com.flink.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfTool {
    private static Properties props = null;
    private static File propfile = null;

    public static Properties ReadConfig(String name) throws FileNotFoundException, IOException{
        String runpath = ConfTool.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        int jarindex = runpath.indexOf(".jar!");
        if(jarindex>0){
            runpath = runpath.substring(0, jarindex);
            int tail = runpath.lastIndexOf("/");
            runpath = runpath.substring(0, tail);
        }else if(runpath.endsWith(".jar")==false && runpath.endsWith("/")) {        	
        	runpath = runpath.substring(0, runpath.length()-1);       	
        }
        String abspath = runpath+"/"+name;
        File cfg = new File(abspath);
        if(cfg.exists()==false){
            throw new FileNotFoundException();
        }
        System.out.println("####"+ abspath );
        propfile = cfg;

        props = new Properties();
        InputStream in = new FileInputStream(cfg);
        props.load(in);
        return props;        
    }

    public static File getConfFile(){
        return propfile;
    }

}
